package com.cgbl.cg.vm.course

import android.app.Application
import android.os.Bundle
import android.text.TextUtils
import androidx.databinding.ObservableBoolean
import androidx.databinding.ObservableField
import api.UserServiceFactory
import com.blankj.utilcode.util.LogUtils
import com.robot.base.base.vm.GLBaseViewModel
import com.robot.base.common.api.AbstractViewModelSubscriber
import com.robot.base.configs.AppConstants
import com.robot.base.model.CourseInfoBean
import com.robot.base.model.UserInfoBean
import com.robot.base.util.arouter.ARouterUtil
import me.goldze.mvvmhabit.binding.command.BindingAction
import me.goldze.mvvmhabit.binding.command.BindingCommand
import me.goldze.mvvmhabit.http.BaseResponse

class CourseInfoViewModel(application: Application) : GLBaseViewModel(application) {

    val hideFreeTagFiled = ObservableBoolean()

    val courseFreeField = ObservableField<String>()

    val courseTitleField = ObservableField<String>()

    val userAvatar = ObservableField<String>()

    val teacherFansField = ObservableField<String>()

    val teacherFollowField = ObservableField<String>()

    val teacherEngineField = ObservableField<String>()

    val studyCountField = ObservableField<String>()

    val hourField = ObservableField<String>()

    val statusField = ObservableField<String>()

    val coursePriceField = ObservableField<String>()

    val rulingPriceField = ObservableField<String>()

    val vipPriceField = ObservableField<String>()

    val teacherNameField = ObservableField<String>()

    val courseIntroduceField = ObservableField<String>()

    var isFreeField = ObservableField("1")
    val follow = ObservableField(false)
    val followText = ObservableField<String>("+关注")
    var followerAccountId = ""
    val uc = UiChangeEvent()

    class UiChangeEvent {

    }

    val toUserInfoCommand = BindingCommand<Any>(BindingAction {
        ARouterUtil.goToActivityWithBundle(AppConstants.Router.Mine.A_MINE_OTHER_PERSONAL_PAGE,
            Bundle().apply {
                putString(AppConstants.BundleKey.ID,followerAccountId)
            })
    })
    val followCommand = BindingCommand<Any>(BindingAction {
        followUsers()
    })

    fun setData(data: CourseInfoBean) {
        courseTitleField.set(data.courseName)
        followerAccountId = data.accountId ?: ""
        studyCountField.set("${data.countStudy}人在学")
        hourField.set("总时长${data.classHour}小时")
        statusField.set("${if ((data.classType ?: "0") == "0") "更新中" else "已完结"}（共${data.classHour}课时）")
        coursePriceField.set("￥${data.coursePrice}")
        rulingPriceField.set("原价${data.rulingPrice}")
        courseFreeField.set(if (data.free == "0") "付费" else if (data.free == "2") "限免" else "")
        hideFreeTagFiled.set(TextUtils.isEmpty(courseFreeField.get()))
        courseIntroduceField.set(data.introduce)
        val rulingPrice = if (TextUtils.isEmpty(data.rulingPrice)) "0" else data.rulingPrice
        val classVipPrice = if (TextUtils.isEmpty(data.classVipPrice)) "0" else data.classVipPrice
        vipPriceField.set("立省￥${(rulingPrice!!.toDouble()) - classVipPrice!!.toDouble()}")
        isFreeField.set(data.free)
        fetchTeacherInfo(data.accountId ?: "")
    }

    private fun fetchTeacherInfo(id: String) {
        UserServiceFactory.otherUserInfo(id)
            .subscribe(object : AbstractViewModelSubscriber<BaseResponse<UserInfoBean>>(this) {
                override fun onNext(t: BaseResponse<UserInfoBean>) {
                    userAvatar.set(t.data.head)
                    teacherNameField.set(t.data.nickName)
                    follow.set(t.data.follow)
                    followText.set(
                        if (follow.get() == true) {
                            "已关注"
                        } else {
                            "+关注"
                        }
                    )
                    teacherFansField.set("粉丝 ${t.data.fansCount}")
                    teacherFollowField.set("关注 ${t.data.followCount}")
                }
            })
    }

    fun followUsers() {
        UserServiceFactory.followUsers(
            mapOf(
                "followerAccountId" to followerAccountId
            )
        ).subscribe(object : AbstractViewModelSubscriber<BaseResponse<String>>(this) {
            override fun onNext(t: BaseResponse<String>) {
                LogUtils.e(t)
                follow.set(!(follow.get() ?: false))
                followText.set(
                    if (follow.get() == true) {
                        "已关注"
                    } else {
                        "+关注"
                    }
                )
            }
        })
    }

    companion object {

        const val TYPE_FREE = "1"

        const val TYPE_BUY = "0"

        const val TYPE_VIP_FREE = "2"


    }
}